/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ThemXoaCapNhatPhongBanInternalFrame.java
 *
 * Created on Jul 1, 2010, 9:11:11 AM
 */

package GUI;


import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author May2
 */
public class ThemXoaCapNhatPhongBanInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form ThemXoaCapNhatPhongBanInternalFrame */
    public ThemXoaCapNhatPhongBanInternalFrame() {
        initComponents();
    }

    public void searchPhongBan(){
//        String tenPhongBan = this.jtfTenPhongBan.getText();
//        ArrayList<PhongBanPOJO> list = PhongBanDAO.searchPhongBan(tenPhongBan);
//        //Tạo cột trên Jtable
//        DefaultTableModel dtm = new DefaultTableModel();
//        dtm.addColumn("Mã Phòng Ban");
//        dtm.addColumn("Tên Phòng Ban");
//        //Tạo model
//        dtm.setRowCount(list.size());
//        for(int i=0; i<list.size(); i++){
//            dtm.setValueAt(list.get(i).getMaPhongBan(), i, 0);
//            dtm.setValueAt(list.get(i).getTenPhongBan(), i, 1);
//        }
//        //Gán model cho table
//        this.jtbDanhSach.setModel(dtm);
//        this.jtbDanhSach.changeSelection(0, 0, false, false);
    }

    public void hienThiPhongBan(){
//        ArrayList<PhongBanPOJO> list = PhongBanBUS.selectPhongBanAll();
//        //Tạo các cột trên jtbDanhSach
//        DefaultTableModel dtm = new DefaultTableModel();
//        dtm.addColumn("Mã Phòng Ban");
//        dtm.addColumn("Tên Phòng Ban");
//
//        //Tạo model
//        dtm.setRowCount(list.size());
//        for(int i=0; i<list.size(); i++){
//            dtm.setValueAt(list.get(i).getMaPhongBan(), i, 0);
//            dtm.setValueAt(list.get(i).getTenPhongBan(), i, 1);
//        }
//
//        //Gán model cho JTable
//        this.jtbDanhSach.setModel(dtm);
//        this.jtbDanhSach.changeSelection(0, 0, false, false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlbTenPhongBan = new javax.swing.JLabel();
        jtfTenPhongBan = new javax.swing.JTextField();
        jbtTim = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDanhSach = new javax.swing.JTable();
        jbtThem = new javax.swing.JButton();
        jbtXoa = new javax.swing.JButton();
        jbtCapNhat = new javax.swing.JButton();
        jbtLen = new javax.swing.JButton();
        jbtXuong = new javax.swing.JButton();
        jbtThoat = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("THÊM - XÓA - CẬP NHẬT PHÒNG BAN");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jlbTenPhongBan.setText("Tên Phòng Ban");

        jtfTenPhongBan.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfTenPhongBanCaretUpdate(evt);
            }
        });

        jbtTim.setText("Tìm");
        jbtTim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtTimActionPerformed(evt);
            }
        });

        jtbDanhSach.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jtbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbDanhSach);

        jbtThem.setText("Thêm");
        jbtThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThemActionPerformed(evt);
            }
        });

        jbtXoa.setText("Xóa");
        jbtXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXoaActionPerformed(evt);
            }
        });

        jbtCapNhat.setText("Cập Nhật");
        jbtCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCapNhatActionPerformed(evt);
            }
        });

        jbtLen.setText("Lên");
        jbtLen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtLenActionPerformed(evt);
            }
        });

        jbtXuong.setText("Xuống");
        jbtXuong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXuongActionPerformed(evt);
            }
        });

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jlbTenPhongBan)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfTenPhongBan, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jbtTim, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbtCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtThem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtLen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtXuong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbTenPhongBan)
                    .addComponent(jtfTenPhongBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtTim))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtThem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtXoa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtCapNhat)
                        .addGap(38, 38, 38)
                        .addComponent(jbtLen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtXuong)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtThoat))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(44, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
//        ArrayList<PhongBanPOJO> list = PhongBanDAO.selectPhongBanAll();
//        //Tạo các cột trên JTable
//        DefaultTableModel dtm = new DefaultTableModel();
//        dtm.addColumn("Mã Phòng Ban");
//        dtm.addColumn("Tên Phòng Ban");
//
//        //Tạo model
//        dtm.setRowCount(list.size());
//        for(int i=0; i<list.size(); i++){
//            dtm.setValueAt(list.get(i).getMaPhongBan(), i, 0);
//            dtm.setValueAt(list.get(i).getTenPhongBan(), i, 1);
//        }
//
//        //Gán model vào table
//        this.jtbDanhSach.setModel(dtm);
//        this.jtbDanhSach.changeSelection(0, 0, false, false);
    }//GEN-LAST:event_formInternalFrameOpened

    private void jbtTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtTimActionPerformed
        // TODO add your handling code here:
        this.searchPhongBan();
    }//GEN-LAST:event_jbtTimActionPerformed

    private void jtfTenPhongBanCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfTenPhongBanCaretUpdate
        // TODO add your handling code here:
        this.searchPhongBan();
    }//GEN-LAST:event_jtfTenPhongBanCaretUpdate

    private void jbtLenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtLenActionPerformed
        // TODO add your handling code here:
        int dong = this.jtbDanhSach.getSelectedRow();
        dong = dong - 1;
        if(dong >= 0){
            this.jtbDanhSach.changeSelection(dong, 0, false, false);
        } else {
            int dongCuoi = this.jtbDanhSach.getRowCount() - 1;
            this.jtbDanhSach.changeSelection(dongCuoi, 0, false, false);
        }
    }//GEN-LAST:event_jbtLenActionPerformed

    private void jbtXuongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXuongActionPerformed
        // TODO add your handling code here:
        int dong = this.jtbDanhSach.getSelectedRow();
        dong = dong + 1;
        if(dong < this.jtbDanhSach.getRowCount()){
            this.jtbDanhSach.changeSelection(dong, 0, false, false);
        } else {
            this.jtbDanhSach.changeSelection(0, 0, false, false);
        }
    }//GEN-LAST:event_jbtXuongActionPerformed

    private void jbtThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThemActionPerformed
        // TODO add your handling code here:
        ThemPhongBanDialog dlg = new ThemPhongBanDialog(null, true);
        dlg.setVisible(true);
    }//GEN-LAST:event_jbtThemActionPerformed

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        int reply = JOptionPane.showConfirmDialog(this, "Bạn có muốn thoát", "Thông báo", WIDTH);
        if(reply == JOptionPane.OK_OPTION){
            this.dispose();
        }
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void jbtXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXoaActionPerformed
        // TODO add your handling code here:
//        int reply = JOptionPane.showConfirmDialog(this, "Bạn có chắc là muốn xóa?", "Thông báo", WIDTH);
//        if(reply == JOptionPane.OK_OPTION){
//            int dong = this.jtbDanhSach.getSelectedRow();
//            int maPhongBan = (Integer)this.jtbDanhSach.getValueAt(dong, 0);
//            int n = PhongBanDAO.deletePhongBan(maPhongBan);
//            if(n==1){
//                JOptionPane.showMessageDialog(this, "Xóa thành công!!!");
//                this.hienThiPhongBan();
//            } else{
//                JOptionPane.showMessageDialog(this, "Xóa thất bại.");
//            }
//        }
        
    }//GEN-LAST:event_jbtXoaActionPerformed

    private void jbtCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCapNhatActionPerformed
        // TODO add your handling code here:
//        int dong = this.jtbDanhSach.getSelectedRow();
//        if(dong>=0){
//            CapNhatPhongBanDialog dlg = new CapNhatPhongBanDialog(null, true);
//            PhongBanPOJO pb = new PhongBanPOJO();
//            pb.setMaPhongBan((Integer)this.jtbDanhSach.getValueAt(dong, 0));
//            pb.setTenPhongBan((String)this.jtbDanhSach.getValueAt(dong, 1));
//            dlg.setPb(pb);
//            dlg.setVisible(true);
//            //
//            pb.setMaPhongBan(dlg.getMaPhongBan());
//            pb.setTenPhongBan(dlg.getTenPhongBan());
//            PhongBanBUS.updatePhongBan(pb);
//            this.hienThiPhongBan();
//        }
    }//GEN-LAST:event_jbtCapNhatActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtCapNhat;
    private javax.swing.JButton jbtLen;
    private javax.swing.JButton jbtThem;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JButton jbtTim;
    private javax.swing.JButton jbtXoa;
    private javax.swing.JButton jbtXuong;
    private javax.swing.JLabel jlbTenPhongBan;
    private javax.swing.JTable jtbDanhSach;
    private javax.swing.JTextField jtfTenPhongBan;
    // End of variables declaration//GEN-END:variables

}
